﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BloomsburyBanHang.DTO;

namespace BloomsburyBanHang.DAO
{
    public class HoaDonDAO
    {
        public HoaDonDAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static List<HOA_DON> laydsHoaDonTheoTenKhachHang(string tenKH)
        {
            NhaBanLeDataContext db = new NhaBanLeDataContext();
            List<HOA_DON> ds = null;
            var query = from hd in db.HOA_DONs
                        where hd.KHACH_HANG.TenKhachHang == tenKH
                        select hd;
            ds = query.ToList<HOA_DON>();
            return ds;
        }
        public static List<HOA_DON> laydsHoaDonTheoTenDangNhap(string tenDN)
        {
            NhaBanLeDataContext db = new NhaBanLeDataContext();
            List<HOA_DON> ds = null;
            var query = from hd in db.HOA_DONs
                        where hd.KHACH_HANG.TenDangNhap == tenDN
                        select hd;
            ds = query.ToList<HOA_DON>();
            return ds;
        }
        public static int DatHang(List<CHI_TIET_HOA_DON> listCTHD, string tenDN)
        {
            NhaBanLeDataContext db = new NhaBanLeDataContext();
            HOA_DON hd = new HOA_DON();
            int maKhachHang = Int32.Parse(tenDN);
            hd.KHACH_HANG = KhachHangDAO.TimKhachHangTheoMa(tenDN);
            hd.NgayDatHang = DateTime.Today;
            //hd.NgayGiaoHang = DateTime.Today.AddDays(4);
            hd.NgayGiaoHang = DateTime.Now;
            hd.NHA_VAN_CHUYEN = null;
            hd.TongTien = 0;
            for (int i = 0; i < listCTHD.Count; i++)
            {
                hd.CHI_TIET_HOA_DONs.Add(listCTHD[i]);
            }
            db.HOA_DONs.Attach(hd);
            db.SubmitChanges();
            return db.HOA_DONs.Last().MaHoaDon;
        }

        public static List<CHI_TIET_HOA_DON> TimDonDatHangTheoMa(int maDonDatHang)
        {
            NhaBanLeDataContext db = new NhaBanLeDataContext();
            List<CHI_TIET_HOA_DON> list = null;
            var query = (from hd in db.HOA_DONs
                         where hd.MaHoaDon == maDonDatHang
                         select hd.CHI_TIET_HOA_DONs).First();
            list = query.ToList<CHI_TIET_HOA_DON>();
            return list;

        }

        public static int XoaHoaDonTheoMa(int maDonDatHang)
        {
            int kq = -1;
            NhaBanLeDataContext db = new NhaBanLeDataContext();
            try
            {
                var query = from hd in db.HOA_DONs where hd.MaHoaDon == maDonDatHang select hd;
                HOA_DON hoadon = query.First<HOA_DON>();
                System.Data.Linq.Table<HOA_DON> table = db.GetTable<HOA_DON>();
                table.DeleteOnSubmit(hoadon);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static HoaDonDTO layHoaDonTheoMa(int maHoaDon)
        {
            NhaBanLeDataContext db = new NhaBanLeDataContext();
          
            var query = from hd in db.HOA_DONs
                        where hd.MaHoaDon == maHoaDon
                        select hd;
            HOA_DON hoadon = query.First<HOA_DON>();
            return HoaDonDTO.Convert(hoadon);
           
        }
    }
}